package cn.nexd.app.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nexd.app.model.UserInfo;

/**
 * Created by xun on 2016/5/13.
 */
public class MySqlLiteOpenHelper extends SQLiteOpenHelper {
    public static final String dbName = "db_nexd.db";
    private SQLiteDatabase db;
    private final static int curVersion = 1;
    private String table1 = "user";
    private MapModel mapModel;

    public MySqlLiteOpenHelper(Context context) {
        super(context, dbName, null, curVersion);
        db = getSqLiteDatabase();
    }

    public SQLiteDatabase getSqLiteDatabase() {
        return db = getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table  if not exists " + table1 + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,userMail,userName,userPassword)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            switch (curVersion) {
                //更新
            }
        }
    }

    public Cursor selectCursor(String sql, String[] selectArgs) {
        return db.rawQuery(sql, selectArgs);
    }

    //添加多条数据
    public int insert(List<Map<String, String>> list) {
        try {
            db.beginTransaction();
            for (Map<String, String> map : list) {
                ContentValues values = mapToContentValues(map);
                db.insert(table1, null, values);
                if (values != null) {
                    values.clear();
                }
            }
            db.setTransactionSuccessful();
            return list.size();
        } catch (Exception e) {
            return -1;
        } finally {
          db.endTransaction();
        }
    }

    //更新数据
    public int update(ContentValues values, String stringWhere, String[] stringArgs) {
        try {
            int rowNum = db.update(table1, values, stringWhere, stringArgs);
            return rowNum;
        } catch (Exception e) {
            return -1;
        } finally {
        }
    }

    //添加一条数据
    public int insert(TToMap tToMap) {
        try {
            ContentValues contentValues = tToMap.toMap();
            db.insert(table1, null, contentValues);
            return 1;
        } catch (Exception e) {
            return -1;
        } finally {
        }
    }

    public int insert(ContentValues values) {
        try {
            db.insert(table1, null, values);
            return 1;
        } catch (Exception e) {
            return -1;
        } finally {
        }
    }


    public int delete(String sqlWhere, String[] stringArgs) {
        try {
            db.beginTransaction();
            int rowCount = db.delete(table1, sqlWhere, stringArgs);
            db.setTransactionSuccessful();
            return rowCount;
        } catch (Exception e) {
            return -1;
        } finally {
            db.endTransaction();
        }

    }


    public interface TToMap {
        ContentValues toMap();
    }

    public void close(Cursor cursor) {
        if (db != null) {
            db.close();
        }
        if (cursor != null) {
            cursor.close();
        }
    }


    public ContentValues mapToContentValues(Map<String, String> map) {
        ContentValues values = new ContentValues();
        for (String key : map.keySet()) {
            values.put(key, map.get(key).toString());
        }
        return values;
    }


    public <T> List<T> query(String sql, String[] stringArgs, ModelT<T> modelT) {
        List<T> list = new ArrayList<T>();
        try {
            Cursor cursor = selectCursor(sql, stringArgs);
            while (cursor.moveToNext()) {
                list.add(modelT.Model(cursor));
            }
            return list;
        } catch (Exception e) {
            return list;
        } finally {
            close();
        }
    }

    public List<Map<String, Object>> selectCursorToList(Cursor cursor) {
        mapModel = new MapModel();
        List<Map<String, Object>> selectResult = new ArrayList<Map<String, Object>>();
        try {
            while (cursor.moveToNext()) {
                selectResult.add(mapModel.Model(cursor));
            }
            return selectResult;
        } catch (Exception e) {
            return selectResult;
        } finally {
            close();
        }
    }

    public <T> Map<String, T> selectCursorToMap(Cursor cursor, ModelT modelT) {
        Map<String, T> map = new HashMap<String, T>();
        try {
            while (cursor.moveToNext()) {
                map.putAll((Map<? extends String, ? extends T>) modelT.Model(cursor));
            }
            return map;
        } catch (Exception e) {
            return map;
        } finally {
            close();
        }
    }


    public interface ModelT<T> {
        T Model(Cursor cursor);
    }


    class MapModel implements ModelT<Map<String, Object>> {

        @Override
        public Map<String, Object> Model(Cursor cursor) {
            String[] columnNames = cursor.getColumnNames();
            Map<String, Object> user = new HashMap<String, Object>();
            for (String columnName : columnNames) {
                user.put(columnName, cursor.getString(cursor.getColumnIndex(columnName)));
            }
            return user;
        }

    }
}
